/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FEMSolver;

import BounderyPack.Bounderies;
import BounderyPack.Boundery;
import BounderyPack.DBoundery;
import BounderyPack.DirihletPair;
import BounderyPack.SortComparator;
import FEMShapeFunctions.FEMShapeFunction2ElementMatrixGenerator;
import HashMatrix2.HashMatrix2;
import HashMatrix2.LUFactorization;
import Integration.Func2D;
import Integration.IFunction2D;
import MathLib.triangulation.SaveStructures.Node;
import MathLib.triangulation.SaveStructures.Triangle;
import MathLib.triangulation.SaveStructures.TriangleQ;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import syntaxanalyser.PostfiksXY;

/**
 *
 * @author mark_o
 */
public class FEMQuadraticTRMainMatrixGenerator implements FEMGenerator{
    //protected HashMatrix A;
    //protected HashMatrix A;
    protected HashMatrix2 A2;
    protected double[] b;
    protected String F = "sin(x)+cos(y)";
    protected double Kx;
    protected double Ky;
    protected double Pe;
    protected double Pekle;
    protected Bounderies Bounds;
    protected ArrayList<Node> NodeList;
    protected ArrayList<Triangle> TriangleList;
    private double[] tempPyankareF;
    DirihletPair[] mas;

    public FEMQuadraticTRMainMatrixGenerator() {
        Bounds = new Bounderies();
    }

    public void setPe(double Pe) {
        this.Pe = Pe;
    }

    public void setPekle(double Pekle) {
        this.Pekle = Pekle;
    }

    public Bounderies getBounds() {
        return Bounds;
    }

    public void setBounds(Bounderies Bounds) {
        this.Bounds = Bounds;
    }

    public void setKx(double Kx) {
        this.Kx = Kx;
    }

    public void setKy(double Ky) {
        this.Ky = Ky;
    }

    public void setF(String F) {
        this.F = F;
    }

    

    public void GenerateLU(){
        A2 = new HashMatrix2(HashMatrix2.HASH_TYPE, NodeList.size());
        b= new double[NodeList.size()];
        FEMQuadraticTRMatrixGenerator.kx = Kx;
        FEMQuadraticTRMatrixGenerator.ky = Ky;

        FEMQuadraticTRMatrixGenerator.Pex = Pekle;
        FEMQuadraticTRMatrixGenerator.Pey = Pekle;
        FEMShapeFunction2ElementMatrixGenerator fem2gen = new FEMShapeFunction2ElementMatrixGenerator();
        fem2gen.setKx(Kx);
        fem2gen.setKy(Ky);

        for(int i=0;i<TriangleList.size();i++){
            TriangleQ tr = (TriangleQ) TriangleList.get(i);
            fem2gen.setA(NodeList.get(tr.A-1));
            fem2gen.setB(NodeList.get(tr.B-1));
            fem2gen.setC(NodeList.get(tr.C-1));
            fem2gen.setD(NodeList.get(tr.D-1));
            fem2gen.setE(NodeList.get(tr.E-1));
            fem2gen.setF(NodeList.get(tr.F-1));
            fem2gen.update();

            //A
            //double[][] Ke1 = FEMQuadraticTRMatrixGenerator.GenerateKe(NodeList.get(tr.A-1), NodeList.get(tr.B-1), NodeList.get(tr.C-1),NodeList.get(tr.D-1), NodeList.get(tr.E-1), NodeList.get(tr.F-1));
            //double[][] Me = FEMQuadraticTRMatrixGenerator.GenerateMe(NodeList.get(tr.A()-1), NodeList.get(tr.B()-1), NodeList.get(tr.C()-1));
            double[][] Ke = fem2gen.GenerateKe();
            double[][] Me =fem2gen.GenerateMe();
            double[][] Pek = FEMQuadraticTRMatrixGenerator.GeneratePe(NodeList.get(tr.A()-1), NodeList.get(tr.B()-1), NodeList.get(tr.C()-1));
            double[][] PyankareMatrix = this.generateHme(tr);


            for (int in = 0; in < 6; in++) {
            for (int j = 0; j < 6; j++) {
            System.out.print(Ke[in][j]+"\t");
            }
            System.out.println();
            }
            System.out.println("=================================================================================");

            /*for (int in = 0; in < 6; in++) {
            for (int j = 0; j < 6; j++) {
            System.out.print(Pek[in][j]+"\t");
            }
            System.out.println();
            }
            System.out.println("=================================================================================");*/

                /*A2.addValue(tr.A-1, tr.A-1, Ke[0][0] + Pe*Me[0][0]);
            A2.addValue(tr.A-1, tr.B-1, Ke[0][1] + Pe*Me[0][1]);
            A2.addValue(tr.A-1, tr.C-1, Ke[0][2] + Pe*Me[0][2]);
            A2.addValue(tr.A-1, tr.D-1, Ke[0][3] + Pe*Me[0][3]);
            A2.addValue(tr.A-1, tr.E-1, Ke[0][4] + Pe*Me[0][4]);
            A2.addValue(tr.A-1, tr.F-1, Ke[0][5] + Pe*Me[0][5]);

            A2.addValue(tr.B-1, tr.A-1, Ke[1][0] + Pe*Me[1][0]);
            A2.addValue(tr.B-1, tr.B-1, Ke[1][1] + Pe*Me[1][1]);
            A2.addValue(tr.B-1, tr.C-1, Ke[1][2] + Pe*Me[1][2]);
            A2.addValue(tr.B-1, tr.D-1, Ke[1][3] + Pe*Me[1][3]);
            A2.addValue(tr.B-1, tr.E-1, Ke[1][4] + Pe*Me[1][4]);
            A2.addValue(tr.B-1, tr.F-1, Ke[1][5] + Pe*Me[1][5]);

            A2.addValue(tr.C-1, tr.A-1, Ke[2][0] + Pe*Me[2][0]);
            A2.addValue(tr.C-1, tr.B-1, Ke[2][1] + Pe*Me[2][1]);
            A2.addValue(tr.C-1, tr.C-1, Ke[2][2] + Pe*Me[2][2]);
            A2.addValue(tr.C-1, tr.D-1, Ke[2][3] + Pe*Me[2][3]);
            A2.addValue(tr.C-1, tr.E-1, Ke[2][4] + Pe*Me[2][4]);
            A2.addValue(tr.C-1, tr.F-1, Ke[2][5] + Pe*Me[2][5]);

            A2.addValue(tr.D-1, tr.A-1, Ke[3][0] + Pe*Me[3][0]);
            A2.addValue(tr.D-1, tr.B-1, Ke[3][1] + Pe*Me[3][1]);
            A2.addValue(tr.D-1, tr.C-1, Ke[3][2] + Pe*Me[3][2]);
            A2.addValue(tr.D-1, tr.D-1, Ke[3][3] + Pe*Me[3][3]);
            A2.addValue(tr.D-1, tr.E-1, Ke[3][4] + Pe*Me[3][4]);
            A2.addValue(tr.D-1, tr.F-1, Ke[3][5] + Pe*Me[3][5]);

            A2.addValue(tr.E-1, tr.A-1, Ke[4][0] + Pe*Me[4][0]);
            A2.addValue(tr.E-1, tr.B-1, Ke[4][1] + Pe*Me[4][1]);
            A2.addValue(tr.E-1, tr.C-1, Ke[4][2] + Pe*Me[4][2]);
            A2.addValue(tr.E-1, tr.D-1, Ke[4][3] + Pe*Me[4][3]);
            A2.addValue(tr.E-1, tr.E-1, Ke[4][4] + Pe*Me[4][4]);
            A2.addValue(tr.E-1, tr.F-1, Ke[4][5] + Pe*Me[4][5]);

            A2.addValue(tr.F-1, tr.A-1, Ke[5][0] + Pe*Me[5][0]);
            A2.addValue(tr.F-1, tr.B-1, Ke[5][1] + Pe*Me[5][1]);
            A2.addValue(tr.F-1, tr.C-1, Ke[5][2] + Pe*Me[5][2]);
            A2.addValue(tr.F-1, tr.D-1, Ke[5][3] + Pe*Me[5][3]);
            A2.addValue(tr.F-1, tr.E-1, Ke[5][4] + Pe*Me[5][4]);
            A2.addValue(tr.F-1, tr.F-1, Ke[5][5] + Pe*Me[5][5]);*/
                A2.setValue(tr.A-1, tr.A-1, A2.getValue(tr.A-1, tr.A-1) + Ke[0][0]+Pe*Me[0][0] + Pek[0][0] + PyankareMatrix[0][0]);
                A2.setValue(tr.A-1, tr.B-1, A2.getValue(tr.A-1, tr.B-1) + Ke[0][1]+Pe*Me[0][1] + Pek[0][1] + PyankareMatrix[0][1]);
                A2.setValue(tr.A-1, tr.C-1, A2.getValue(tr.A-1, tr.C-1) + Ke[0][2]+Pe*Me[0][2] + Pek[0][2] + PyankareMatrix[0][2]);
                A2.setValue(tr.A-1, tr.D-1, A2.getValue(tr.A-1, tr.D-1) + Ke[0][3]+Pe*Me[0][3] + Pek[0][3] + PyankareMatrix[0][3]);
                A2.setValue(tr.A-1, tr.E-1, A2.getValue(tr.A-1, tr.E-1) + Ke[0][4]+Pe*Me[0][4] + Pek[0][4] + PyankareMatrix[0][4]);
                A2.setValue(tr.A-1, tr.F-1, A2.getValue(tr.A-1, tr.F-1) + Ke[0][5]+Pe*Me[0][5] + Pek[0][5] + PyankareMatrix[0][5]);

                A2.setValue(tr.B-1, tr.A-1, A2.getValue(tr.B-1, tr.A-1) + Ke[1][0]+Pe*Me[1][0] + Pek[1][0] + PyankareMatrix[1][0]);
                A2.setValue(tr.B-1, tr.B-1, A2.getValue(tr.B-1, tr.B-1) + Ke[1][1]+Pe*Me[1][1] + Pek[1][1] + PyankareMatrix[1][1]);
                A2.setValue(tr.B-1, tr.C-1, A2.getValue(tr.B-1, tr.C-1) + Ke[1][2]+Pe*Me[1][2] + Pek[1][2] + PyankareMatrix[1][2]);
                A2.setValue(tr.B-1, tr.D-1, A2.getValue(tr.B-1, tr.D-1) + Ke[1][3]+Pe*Me[1][3] + Pek[1][3] + PyankareMatrix[1][3]);
                A2.setValue(tr.B-1, tr.E-1, A2.getValue(tr.B-1, tr.E-1) + Ke[1][4]+Pe*Me[1][4] + Pek[1][4] + PyankareMatrix[1][4]);
                A2.setValue(tr.B-1, tr.F-1, A2.getValue(tr.B-1, tr.F-1) + Ke[1][5]+Pe*Me[1][5] + Pek[1][5] + PyankareMatrix[1][5]);

                A2.setValue(tr.C-1, tr.A-1, A2.getValue(tr.C-1, tr.A-1) + Ke[2][0]+Pe*Me[2][0] + Pek[2][0] + PyankareMatrix[2][0]);
                A2.setValue(tr.C-1, tr.B-1, A2.getValue(tr.C-1, tr.B-1) + Ke[2][1]+Pe*Me[2][1] + Pek[2][1] + PyankareMatrix[2][1]);
                A2.setValue(tr.C-1, tr.C-1, A2.getValue(tr.C-1, tr.C-1) + Ke[2][2]+Pe*Me[2][2] + Pek[2][2] + PyankareMatrix[2][2]);
                A2.setValue(tr.C-1, tr.D-1, A2.getValue(tr.C-1, tr.D-1) + Ke[2][3]+Pe*Me[2][3] + Pek[2][3] + PyankareMatrix[2][3]);
                A2.setValue(tr.C-1, tr.E-1, A2.getValue(tr.C-1, tr.E-1) + Ke[2][4]+Pe*Me[2][4] + Pek[2][4] + PyankareMatrix[2][4]);
                A2.setValue(tr.C-1, tr.F-1, A2.getValue(tr.C-1, tr.F-1) + Ke[2][5]+Pe*Me[2][5] + Pek[2][5] + PyankareMatrix[2][5]);

                A2.setValue(tr.D-1, tr.A-1, A2.getValue(tr.D-1, tr.A-1) + Ke[3][0]+Pe*Me[3][0] + Pek[3][0] + PyankareMatrix[3][0]);
                A2.setValue(tr.D-1, tr.B-1, A2.getValue(tr.D-1, tr.B-1) + Ke[3][1]+Pe*Me[3][1] + Pek[3][1] + PyankareMatrix[3][1]);
                A2.setValue(tr.D-1, tr.C-1, A2.getValue(tr.D-1, tr.C-1) + Ke[3][2]+Pe*Me[3][2] + Pek[3][2] + PyankareMatrix[3][2]);
                A2.setValue(tr.D-1, tr.D-1, A2.getValue(tr.D-1, tr.D-1) + Ke[3][3]+Pe*Me[3][3] + Pek[3][3] + PyankareMatrix[3][3]);
                A2.setValue(tr.D-1, tr.E-1, A2.getValue(tr.D-1, tr.E-1) + Ke[3][4]+Pe*Me[3][4] + Pek[3][4] + PyankareMatrix[3][4]);
                A2.setValue(tr.D-1, tr.F-1, A2.getValue(tr.D-1, tr.F-1) + Ke[3][5]+Pe*Me[3][5] + Pek[3][5] + PyankareMatrix[3][5]);

                A2.setValue(tr.E-1, tr.A-1, A2.getValue(tr.E-1, tr.A-1) + Ke[4][0]+Pe*Me[4][0] + Pek[4][0] + PyankareMatrix[4][0]);
                A2.setValue(tr.E-1, tr.B-1, A2.getValue(tr.E-1, tr.B-1) + Ke[4][1]+Pe*Me[4][1] + Pek[4][1] + PyankareMatrix[4][1]);
                A2.setValue(tr.E-1, tr.C-1, A2.getValue(tr.E-1, tr.C-1) + Ke[4][2]+Pe*Me[4][2] + Pek[4][2] + PyankareMatrix[4][2]);
                A2.setValue(tr.E-1, tr.D-1, A2.getValue(tr.E-1, tr.D-1) + Ke[4][3]+Pe*Me[4][3] + Pek[4][3] + PyankareMatrix[4][3]);
                A2.setValue(tr.E-1, tr.E-1, A2.getValue(tr.E-1, tr.E-1) + Ke[4][4]+Pe*Me[4][4] + Pek[4][4] + PyankareMatrix[4][4]);
                A2.setValue(tr.E-1, tr.F-1, A2.getValue(tr.E-1, tr.F-1) + Ke[4][5]+Pe*Me[4][5] + Pek[4][5] + PyankareMatrix[4][5]);

                A2.setValue(tr.F-1, tr.A-1, A2.getValue(tr.F-1, tr.A-1) + Ke[5][0]+Pe*Me[5][0] + Pek[5][0] + PyankareMatrix[5][0]);
                A2.setValue(tr.F-1, tr.B-1, A2.getValue(tr.F-1, tr.B-1) + Ke[5][1]+Pe*Me[5][1] + Pek[5][1] + PyankareMatrix[5][1]);
                A2.setValue(tr.F-1, tr.C-1, A2.getValue(tr.F-1, tr.C-1) + Ke[5][2]+Pe*Me[5][2] + Pek[5][2] + PyankareMatrix[5][2]);
                A2.setValue(tr.F-1, tr.D-1, A2.getValue(tr.F-1, tr.D-1) + Ke[5][3]+Pe*Me[5][3] + Pek[5][3] + PyankareMatrix[5][3]);
                A2.setValue(tr.F-1, tr.E-1, A2.getValue(tr.F-1, tr.E-1) + Ke[5][4]+Pe*Me[5][4] + Pek[5][4] + PyankareMatrix[5][4]);
                A2.setValue(tr.F-1, tr.F-1, A2.getValue(tr.F-1, tr.F-1) + Ke[5][5]+Pe*Me[5][5] + Pek[5][5] + PyankareMatrix[5][5]);


            //b
                double[] Fr = fem2gen.GenerateF(new IFunction2D() {

                public double calculate(double x, double y) {
                    try {
                        return PostfiksXY.Calculate(F, x, y);
                    } catch (Exception ex) {
                        Logger.getLogger(FEMQuadraticTRMainMatrixGenerator.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return 0;
                }

                public double calculateDerivatyX(double x, double y) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public double calculateDerivatyY(double x, double y) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
                    /*try {
            Fr[0] = PostfiksXY.Calculate(F, NodeList.get(tr.A-1).x, NodeList.get(tr.A-1).y);
            Fr[1] = PostfiksXY.Calculate(F, NodeList.get(tr.B-1).x, NodeList.get(tr.B-1).y);
            Fr[2] = PostfiksXY.Calculate(F, NodeList.get(tr.C-1).x, NodeList.get(tr.C-1).y);
            Fr[3] = PostfiksXY.Calculate(F, NodeList.get(tr.D-1).x, NodeList.get(tr.D-1).y);
            Fr[4] = PostfiksXY.Calculate(F, NodeList.get(tr.E-1).x, NodeList.get(tr.E-1).y);
            Fr[5] = PostfiksXY.Calculate(F, NodeList.get(tr.F-1).x, NodeList.get(tr.F-1).y);

            } catch (Exception ex) {
            Fr[0] = 0;
            Fr[1] = 0;
            Fr[2] = 0;
            Fr[3] = 0;
            Fr[4] = 0;
            Fr[5] = 0;
            System.out.println("помилка в постфіксі ...");
            }*/
                double[] tmpNayman = getNaymanCond(tr);
                //double Atr = FEMElementMatrixGenerator.ComputeTriangleArea(NodeList.get(tr.A()-1), NodeList.get(tr.B()-1), NodeList.get(tr.C()-1));
                //System.out.println("S"+i+" = "+Atr);
                /*b[tr.A-1]+= Fr[0]/-6.0*Atr + tempPyankareF[0] - tmpNayman[0];
            b[tr.B-1]+= Fr[1]/-6.0*Atr + tempPyankareF[1] - tmpNayman[1];
            b[tr.C-1]+= Fr[2]/-6.0*Atr + tempPyankareF[2] - tmpNayman[2];
            b[tr.D-1]+= Fr[3]/-6.0*Atr + tempPyankareF[3] - tmpNayman[3];
            b[tr.E-1]+= Fr[4]/-6.0*Atr + tempPyankareF[4] - tmpNayman[4];
            b[tr.F-1]+= Fr[5]/-6.0*Atr + tempPyankareF[5] - tmpNayman[5];*/
                b[tr.A-1]+= Fr[0];
                b[tr.B-1]+= Fr[1];
                b[tr.C-1]+= Fr[2];
                b[tr.D-1]+= Fr[3];
                b[tr.E-1]+= Fr[4];
                b[tr.F-1]+= Fr[5];

        }
        //for (int i = 0; i < A2.getN(); i++) {
        //    for (int j = 0; j < A2.getN(); j++) {
        //        System.out.print(A2.getValue(i, j)+"\t");
        //    }
        //    System.out.println();
        //}
        //System.out.println("===================================================================================");
        ApplyDirihlet();
        System.out.println();
        System.out.println("==A=================================================================================");
        for (int i = 0; i < A2.getN(); i++) {
            for (int j = 0; j < A2.getN(); j++) {
                System.out.print(A2.getValue(i, j)+"\t");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println("=B==================================================================================");
        for (int j = 0; j < A2.getN(); j++) {
            System.out.print(b[j]+"\t");
        }
        System.out.println();
        System.out.println("===================================================================================");
    }

    private double[] getNaymanCond(TriangleQ tr){
        
       /* double i = 0,
               j = 0,
               k = 0;
        double h = 0,sigma = 0;
        Node vA = NodeList.get(tr.A-1),
             vB = NodeList.get(tr.B-1),
             vC = NodeList.get(tr.C-1);
        for (int index = 0; index < Bounds.getNayman().size(); index++) {
            Boundery boundery = Bounds.getNayman().get(index);

            i=0;j=0;k=0;

            for (int iter = 0; iter < boundery.getNodeList().size(); iter++) {
                Node temp = boundery.getNodeList().get(iter);

                if(vA.equals(temp)){
                    i=1.0;
                }
                if(vB.equals(temp)){
                    j=1.0;
                }
                if(vC.equals(temp)){
                    k=1.0;
                }

            }
            if(i+j+k>1){
                h = Double.parseDouble(boundery.getParams().h);
                sigma = Double.parseDouble(boundery.getParams().g);
                break;
            }

        }

        double length = i*j*( Math.sqrt( (vA.x - vB.x)*(vA.x - vB.x) + (vA.y - vB.y)*(vA.y - vB.y) ) ) +
                        k*j*( Math.sqrt( (vC.x - vB.x)*(vC.x - vB.x) + (vC.y - vB.y)*(vC.y - vB.y) ) ) +
                        i*k*( Math.sqrt( (vA.x - vC.x)*(vA.x - vC.x) + (vA.y - vC.y)*(vA.y - vC.y) ) );

        //tempPyankareF = new double[]{i*h*sigma*length/2.0, j*h*sigma*length/2.0, k*h*sigma*length/2.0};
            length*=h/6.0;

        return new double[]{
             i/2.0*h*length , j/2.0*h*length , k/2.0*h*length, i/2.0*h*length , j/2.0*h*length , k/2.0*h*length
        };
        */
        return new double[6];
    }


    private void ApplyDirihlet(){
        ArrayList<DirihletPair> tempList = new ArrayList<DirihletPair>();
        for (int index = 0; index < Bounds.getDirihlet().size(); index++) {
            DBoundery boundery = Bounds.getDirihlet().get(index);
            for (int iter = 0; iter < boundery.getNodeList().size(); iter++) {
                try {
                    //this.Set(boundery.getNodeList().get(iter).Number, );
                    tempList.add(new DirihletPair(boundery.getNodeList().get(iter).Number, PostfiksXY.Calculate("(" + boundery.getParams().g + ")/(" + boundery.getParams().h + ")", boundery.getNodeList().get(iter).x, boundery.getNodeList().get(iter).y)));
                } catch (Exception ex) {
                    Logger.getLogger(FEMMainMatrixGenerator.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.print("помилка");
                }
            }
        }
        mas = new DirihletPair[tempList.size()];
        tempList.toArray(mas);
        Arrays.sort(mas, new SortComparator());

        /*for (int i = 0; i < mas.length; i++) {
        System.out.println(mas[i]);
        }*/

        for (int i = mas.length-1; i >-1 ; i--) {
            DirihletPair dirihletPair = mas[i];

            for (int j = 0; j < A2.getN(); j++) {
                b[j]-=dirihletPair.getValue()*A2.getValue(j, dirihletPair.getNodeNumber());
                //A2.setValue(j, dirihletPair.getNodeNumber(), 0);
            }


            //видалення непотрібного ....
                double[] dtemp = new double[b.length-1];
                for (int j = 0; j < mas[i].getNodeNumber(); j++) {
                    dtemp[j] = b[j];
                }
                for (int j = mas[i].getNodeNumber()+1; j < b.length; j++) {
                    dtemp[j-1] = b[j];
                }
                b = dtemp;
                A2.removeRow(mas[i].getNodeNumber());

        }

    }

    private double[][] generateHme(TriangleQ tr){
        tempPyankareF = new double[6];
        /*double i = 0,
               j = 0,
               k = 0;
        double h = 0,sigma = 0;
        Node vA = NodeList.get(tr.A-1),
             vB = NodeList.get(tr.B-1),
             vC = NodeList.get(tr.C-1);
        for (int index = 0; index < Bounds.getPyankare().size(); index++) {
            Boundery boundery = Bounds.getPyankare().get(index);

            i=0;j=0;k=0;

            for (int iter = 0; iter < boundery.getNodeList().size(); iter++) {
                Node temp = boundery.getNodeList().get(iter);

                if(vA.equals(temp)){
                    i=1.0;
                }
                if(vB.equals(temp)){
                    j=1.0;
                }
                if(vC.equals(temp)){
                    k=1.0;
                }

            }
            if(i+j+k>1){
                h = Double.parseDouble(boundery.getParams().h);
                sigma = Double.parseDouble(boundery.getParams().sigma);
                break;
            }

        }

        double length = i*j*( Math.sqrt( (vA.x - vB.x)*(vA.x - vB.x) + (vA.y - vB.y)*(vA.y - vB.y) ) ) +
                        k*j*( Math.sqrt( (vC.x - vB.x)*(vC.x - vB.x) + (vC.y - vB.y)*(vC.y - vB.y) ) ) +
                        i*k*( Math.sqrt( (vA.x - vC.x)*(vA.x - vC.x) + (vA.y - vC.y)*(vA.y - vC.y) ) );

        tempPyankareF = new double[]{i*h*sigma*length/2.0, j*h*sigma*length/2.0, k*h*sigma*length/2.0, i*h*sigma*length/2.0, j*h*sigma*length/2.0, k*h*sigma*length/2.0};
            length*=h/6.0;
         * */
            return new double[6][6];
        /*return new double[][]{
        { i*2*length , i*j*length , i*k*length},
        { i*j*length , j*2*length , j*k*length},
        { i*k*length , k*j*length , k*2*length}
        };*/

    }

    public void Set(int pos,double f){
        this.A2.setValue(pos, pos, DrawingPanels.FEM_DRAWING_CONSTANTS.FEM_MULTI_CONST_BOUNDERY);
        this.b[pos]+=f*DrawingPanels.FEM_DRAWING_CONSTANTS.FEM_MULTI_CONST_BOUNDERY;
    }

      public void SolveLU(String outPath) throws FileNotFoundException{
        LUFactorization g = new LUFactorization(A2);
        double[] res = g.calculate(b);
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(outPath)));
        pw.println(res.length + mas.length);
        if(mas.length>0){
            int j;
            for (j = 0; j < mas[0].getNodeNumber(); j++) {
                pw.println(res[j]);
            }

            for(int i=0; i<mas.length-1; i++){
                int k=0;
                pw.println(mas[i].getValue());
                while(mas[i+1].getNodeNumber() - mas[i].getNodeNumber()-k!=1){
                    pw.println(res[j]);
                    j++;
                    k++;
                }
            }

            pw.println(mas[mas.length-1].getValue());

            for (int i = j; i < res.length; i++) {
                pw.println(res[i]);
            }
        }else{
            for (int i = 0; i < res.length; i++) {
                pw.println(res[i]);
            }
        }
        pw.close();
    }

    public void Generate() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void SolveG(String outPath) throws FileNotFoundException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setTriangleList(ArrayList<Triangle> tr) {
        this.TriangleList = tr;
    }

    public void setNodeList(ArrayList<Node> nd) {
        this.NodeList = nd;
    }
}
